<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <div class="top-navbar">
      <div class="navbar-content">
        <div class="logo-section">
          <img src="@/assets/logo/logo.png" alt="Logo" class="logo" />
          <h1 class="site-title">社团联盟</h1>
        </div>
        <div class="nav-actions">
          <el-button type="primary" @click="goToLogin" class="nav-btn login-btn">
            <el-icon><User /></el-icon>
            登录
          </el-button>
          <el-button type="success" @click="goToRegister" class="nav-btn register-btn">
            <el-icon><UserFilled /></el-icon>
            注册
          </el-button>
        </div>
      </div>
    </div>

    <!-- 主横幅区域 -->
    <div class="hero-banner">
      <div class="hero-content">
        <h2 class="hero-title">发现精彩社团生活</h2>
        <p class="hero-subtitle">加入我们，遇见志同道合的朋友，开启美好的校园时光</p>
        <div class="hero-stats">
          <div class="stat-item">
            <div class="stat-number">{{ clubs.length }}</div>
            <div class="stat-label">活跃社团</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ activities.length }}</div>
            <div class="stat-label">精彩活动</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="main-content">
      <!-- 社团列表区域 -->
      <div class="section clubs-section">
        <div class="section-header">
          <h3 class="section-title">
            <el-icon><OfficeBuilding /></el-icon>
            热门社团
          </h3>
          <el-button type="text" @click="viewAllClubs" class="view-all-btn">
            查看全部 <el-icon><ArrowRight /></el-icon>
          </el-button>
        </div>
        <div class="clubs-grid">
          <div v-for="club in clubs.slice(0, 6)" :key="club.clubId" class="club-card">
            <div class="club-cover">
              <img :src="getImageUrl(club.logo) || defaultLogo" class="club-avatar" />
              <div class="club-overlay">
                <el-button type="primary" @click="handleApply(club)" class="apply-btn">
                  申请加入
                </el-button>
              </div>
            </div>
            <div class="club-info">
              <h4 class="club-name">{{ club.clubName }}</h4>
              <p class="club-desc">{{ truncateText(club.description, 50) }}</p>
              <div class="club-meta">
                <span class="member-count">
                  <el-icon><User /></el-icon>
                  {{ club.memberCount || 0 }} 成员
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 活动列表区域 -->
      <div class="section activities-section">
        <div class="section-header">
          <h3 class="section-title">
            <el-icon><Calendar /></el-icon>
            近期活动
          </h3>
          <el-button type="text" @click="viewAllActivities" class="view-all-btn">
            查看全部 <el-icon><ArrowRight /></el-icon>
          </el-button>
        </div>
        <div class="activities-grid">
          <div v-for="activity in activities.slice(0, 4)" :key="activity.activityId" class="activity-card">
            <div class="activity-date">
              <div class="date-day">{{ formatDay(activity.startTime) }}</div>
              <div class="date-month">{{ formatMonth(activity.startTime) }}</div>
            </div>
            <div class="activity-content">
              <h4 class="activity-title">{{ activity.title }}</h4>
              <p class="activity-desc">{{ truncateText(activity.description, 80) }}</p>
              <div class="activity-meta">
                <span class="activity-location">
                  <el-icon><Location /></el-icon>
                  {{ activity.location || '待定' }}
                </span>
                <span class="activity-club">
                  <el-icon><OfficeBuilding /></el-icon>
                  {{ activity.clubName }}
                </span>
              </div>
            </div>
            <div class="activity-actions">
              <el-button type="primary" size="small" @click="handleJoinActivity(activity)">
                参与活动
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部信息 -->
    <div class="footer">
      <div class="footer-content">
        <p>&copy; 2024 社团联盟系统. All rights reserved.</p>
      </div>
    </div>
  </div>
</template>

<script>
import { listClubsByVisitor } from '@/api/system/clubs'
import { listPublicActivities } from '@/api/system/activities'
import useUserStore from '@/store/modules/user'
import defaultLogo from '@/assets/logo/logo.png'
import { 
  User, 
  UserFilled, 
  OfficeBuilding, 
  Calendar, 
  ArrowRight, 
  Location 
} from '@element-plus/icons-vue'

export default {
  name: 'Home',
  components: {
    User,
    UserFilled,
    OfficeBuilding,
    Calendar,
    ArrowRight,
    Location
  },
  data() {
    return {
      clubs: [],
      activities: [],
      defaultLogo
    }
  },
  created() {
    this.loadClubs()
    this.loadActivities()
  },
  methods: {
    getImageUrl(url) {
      if (!url) return '';
      if (url.startsWith('http://') || url.startsWith('https://')) {
        return url;
      }
      return import.meta.env.VITE_APP_BASE_API + url;
    },
    loadClubs() {
      listClubsByVisitor().then(res => {
        this.clubs = res.rows || []
      }).catch(err => {
        console.error('加载社团列表失败:', err)
      })
    },
    loadActivities() {
      listPublicActivities({ pageSize: 10 }).then(res => {
        this.activities = res.rows || []
      }).catch(err => {
        console.error('加载活动列表失败:', err)
      })
    },
    handleApply(club) {
      const userStore = useUserStore()
      const token = userStore.token
      if (!token) {
        this.$message.info('请先登录后再申请加入社团')
        this.$router.push({ path: '/login', query: { redirect: '/home' } })
        return
      }
      // 跳转到社团申请页或弹窗
      this.$router.push({ path: `/index`, query: { clubId: club.clubId } })
    },
    handleJoinActivity(activity) {
      const userStore = useUserStore()
      const token = userStore.token
      if (!token) {
        this.$message.info('请先登录后再参与活动')
        this.$router.push({ path: '/login', query: { redirect: '/home' } })
        return
      }
      // 跳转到活动详情页
      this.$router.push({ path: `/index`, query: { activityId: activity.activityId } })
    },
    goToLogin() {
      this.$router.push('/login')
    },
    goToRegister() {
      this.$router.push('/register')
    },
    viewAllClubs() {
      this.$router.push('/index')
    },
    viewAllActivities() {
      this.$router.push('/index')
    },
    truncateText(text, length) {
      if (!text) return ''
      return text.length > length ? text.substring(0, length) + '...' : text
    },
    formatDay(dateStr) {
      if (!dateStr) return ''
      const date = new Date(dateStr)
      return date.getDate().toString().padStart(2, '0')
    },
    formatMonth(dateStr) {
      if (!dateStr) return ''
      const date = new Date(dateStr)
      const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
      return months[date.getMonth()]
    }
  }
}
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
}

/* 顶部导航栏 */
.top-navbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid transparent;
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.navbar-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 70px;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 40px;
  height: 40px;
  border-radius: 8px;
}

.site-title {
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.nav-actions {
  display: flex;
  gap: 20px;
}

.nav-btn {
  padding: 10px 20px;
  border-radius: 25px;
  font-weight: 500;
  transition: all 0.3s ease;
  border: none;
}

.login-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.register-btn {
  background: linear-gradient(135deg, #11998e, #38ef7d);
  color: white;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(17, 153, 142, 0.4);
}

/* 主横幅区域 */
.hero-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 80px 20px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.hero-banner::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 100" fill="%23ffffff" opacity="0.1"><polygon points="1000,100 1000,0 0,100"/></svg>');
  background-size: cover;
}

.hero-content {
  max-width: 800px;
  margin: 0 auto;
  position: relative;
  z-index: 1;
}

.hero-title {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.hero-subtitle {
  font-size: 20px;
  margin-bottom: 40px;
  opacity: 0.9;
  line-height: 1.6;
}

.hero-stats {
  display: flex;
  justify-content: center;
  gap: 60px;
  margin-top: 40px;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 8px;
  color: #fff;
}

.stat-label {
  font-size: 16px;
  opacity: 0.8;
}

/* 主内容区域 */
.main-content {
  background: #f8fafc;
  padding: 80px 20px;
}

.section {
  max-width: 1200px;
  margin: 0 auto 80px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.section-title {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0;
}

.section-title .el-icon {
  color: #667eea;
  font-size: 32px;
}

.view-all-btn {
  color: #667eea;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.3s ease;
}

.view-all-btn:hover {
  color: #764ba2;
  transform: translateX(4px);
}

/* 社团网格 */
.clubs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 30px;
}

.club-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
}

.club-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.club-cover {
  position: relative;
  height: 200px;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex;
  align-items: center;
  justify-content: center;
}

.club-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 4px solid transparent;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  object-fit: cover;
}

.club-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.3s ease;
}

.club-card:hover .club-overlay {
  opacity: 1;
}

.apply-btn {
  background: white;
  color: #667eea;
  border: none;
  padding: 12px 24px;
  border-radius: 25px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.apply-btn:hover {
  background: #667eea;
  color: white;
  transform: scale(1.05);
}

.club-info {
  padding: 24px;
}

.club-name {
  font-size: 20px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.club-desc {
  color: #64748b;
  line-height: 1.6;
  margin: 0 0 16px 0;
}

.club-meta {
  display: flex;
  align-items: center;
  gap: 16px;
}

.member-count {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #64748b;
  font-size: 14px;
}

/* 活动网格 */
.activities-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
}

.activity-card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  display: flex;
  gap: 20px;
  align-items: flex-start;
}

.activity-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.activity-date {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  padding: 16px;
  border-radius: 12px;
  text-align: center;
  min-width: 80px;
  flex-shrink: 0;
}

.date-day {
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
}

.date-month {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 4px;
}

.activity-content {
  flex: 1;
}

.activity-title {
  font-size: 18px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.activity-desc {
  color: #64748b;
  line-height: 1.6;
  margin: 0 0 12px 0;
}

.activity-meta {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.activity-location,
.activity-club {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #64748b;
  font-size: 14px;
}

.activity-actions {
  flex-shrink: 0;
  display: flex;
  align-items: flex-start;
}

/* 底部 */
.footer {
  background: #2c3e50;
  color: white;
  padding: 40px 20px;
  text-align: center;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
}

.footer-content p {
  margin: 0;
  opacity: 0.8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .hero-title {
    font-size: 32px;
  }
  
  .hero-stats {
    flex-direction: column;
    gap: 30px;
  }
  
  .clubs-grid {
    grid-template-columns: 1fr;
  }
  
  .activities-grid {
    grid-template-columns: 1fr;
  }
  
  .activity-card {
    flex-direction: column;
    text-align: center;
  }
  
  .navbar-content {
    padding: 0 16px;
  }
  
  .nav-actions {
    gap: 8px;
  }
  
  .nav-btn {
    padding: 8px 16px;
    font-size: 14px;
  }
}
</style>